package examples.webstore.domainobject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import examples.webstore.Database;


public class ProductList {
	
	private List<Product> products = new ArrayList<Product>(); 
	
	static private ProductList productList = new ProductList();

	
	private ProductList(){
		products = Database.products;
	}
	
	/**
	 * 
	 */
	public static ProductList getInstance()	{
		return productList;
	}
	
	/**
	 * @param zipCode
	 * @return
	 */
	public List<Product> displayList(long zipCode)	{
		
		if(zipCode <= 0){
			return products;
		}
		
		List<Product> returnList = new ArrayList<Product>();
		
		for(int i=0, length = products.size(); i < length; i++)	{
			if(products.get(i).getZipCode() == zipCode){
				returnList.add(products.get(i));
			}
		}
		
		return returnList;
	}
	
	
	/**
	 * @param zipCode
	 * @return
	 */
	public Product getProduct(String name)	{
		
		for(int i=0, length = products.size(); i < length; i++)	{
			if(products.get(i).getName().equalsIgnoreCase(name)){
				return products.get(i);
			}
		}
		
		return null;
	
	}
	
	/**
	 * @param product
	 */
	public void add(Product product)	{
		if(product != null){
			products.add(product);
		}
	}
	
	/**
	 * 
	 */
	public int getProductCountByUser(String userName, Date addedAfter)	{
		
		int count = 0;
		for(int i=0, length = products.size(); i < length; i++)	{
			Product product = products.get(i);
			
			if(product.getSellerName().equalsIgnoreCase(userName) && addedAfter.before(product.getAddedOn())){
				count++;
			}
		}
		
		return count !=0 ? count++ : count;
		
	}

}
